<?php
/**
 * Created by PhpStorm.
 * User: cqfut
 * Date: 2018/11/1
 * Time: 12:33
 */
@session_start();
require_once (ROOTDIR."/common/router_utils.php");

class BaseRouter {
    private $reqData; // 请求参数
    private $rspData; // 返回参赛


    public function __construct() {
        $this->reqData=array();
        $this->rspData=array();
        $this->rspData['dat'] = array();
        BaseRouter::setRspStatus(1);
    }

    public function getSession($name ) {
        $val = null;
        if ( isset($_SESSION[$name]) ) {
            $val = $_SESSION[$name];
        }
        return $val;
    }

    public function checkAuth($privilegeNames ) {
        $allow = false;
        foreach ( $privilegeNames as $pName ) {
            if ( isset($_SESSION[$pName]) ) {
                $allow = true;
                break;
            }
        }
        return $allow;
    }

    public function doFileUpload($formId, $saveDir ) {
        $uploadFile=$_FILES[$formId];
        $fileType=$uploadFile["type"];
        $supportTypes=array(
            'application/vnd.ms-excel',
            'text/plain',
            'application/msword',
            'application/zip',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'application/octet-stream'
        );
        $fileSize = $uploadFile["size"];
        $fileErr = $uploadFile["error"];
        // 检查上传的文件是否有效
        if ( !in_array($fileType, $supportTypes) ) {
            $this->setRspStatus(0, -1002,"文件类型错误");
            return null;
        }

        if (  $fileSize > 10 * 1024 * 1024 ) {
            $this->setRspStatus(0, -1003,"文件大小超限,最大只能上传10M的文件");
            return null;
        }

        if ( $fileErr > 0 ) {
            $this->setRspStatus(0, -1004,"上传文件错误");
            return null;
        }

        $fileName = $uploadFile["name"];
        $filePath = $saveDir.BaseRouter::getSession('user_id').$fileName;
        // 防止中文乱码
        $filePath = iconv("UTF-8", "gb2312",$filePath);
        // 删除同名文件
        if( file_exists($filePath) ) {
            unlink($filePath);
        }
        $ret = move_uploaded_file($uploadFile["tmp_name"], $filePath);
        if ( !$ret ) {
            $this->setRspStatus(0, -1,"保存文件失败");
            return null;
        }
        return $filePath;
    }

    public function init() {
        $this->reqData = readReqData();
    }

    public function run() {

    }

    public function checkParam($args) {
        $ret = true;
        foreach ( $args as $arg) {
            $val = BaseRouter::getReqVal($arg);
            if ( $val == null || $val == "" ) {
                $ret = true;
                break;
            }
        }
        return $ret;
    }

    public function setRspStatus($status = 0, $errCode = null, $errMsg = null) {
        if ( !is_null($status) ) {
            $this->rspData['status'] = $status;
        }
        if ( !is_null($errCode) ) {
            $this->rspData['code'] = $errCode;
        }
        if ( !is_null($errMsg) ) {
            $this->rspData['msg'] = urlencode($errMsg);
        }
    }

    public function setRspVal($name, $val) {
        $this->rspData['dat'][$name] = $val;
    }

    public function setRspData($retData) {
        $this->rspData['dat'] = $retData;
    }

    // 获取请求的字符串参数值
    public function getReqVal($name) {
        $val = null;
        if ( isset($this->reqData[$name]) ) {
            $val = $this->reqData[$name];
        }
        return $val;
    }

    // 获取请求的整型参数值 -1 表示无效值
    public function getReqIntVal($name) {
        $intVal = $this->getReqVal($name);
        if ( $intVal !== null && strlen($intVal) > 0 ) {
            $intVal = intval($intVal);
        }
        else {
            $intVal = -1;
        }
        return $intVal;
    }

    // 获取请求的字符串参数值 ""表示无效值
    public function getReqStrVal($name) {
        $strVal = $this->getReqVal($name);
        if ( $strVal === null ) {
            $strVal = "";
        }
        return $strVal;
    }

    public function finish() {
        writeRspData($this->rspData);
    }
}